﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Gomoku
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        private enum MoveState
        {
            PLAYER, AI
        }

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Table table;
        AIPlayer aiPlayer;
        MoveState moveState;

        FieldType currentFieldType = FieldType.WHITE;
        MouseState mouseStateCurrent;
        MouseState mouseStatePrevious;

        SpriteFont font;
        string playerInfo;
        string winInfo;

        Checker lastPuttedChecker = null;
        Texture2D winBackground;

        Boolean win = false;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.IsFullScreen = false;
            graphics.PreferredBackBufferHeight = 570;
            graphics.PreferredBackBufferWidth = 570;
            Content.RootDirectory = "Content";
        }


        protected override void Initialize()
        {
            table = new Table(Table.TableSize.SMALL);
            aiPlayer = new AIPlayer(table);
            moveState = MoveState.PLAYER;
            this.IsMouseVisible = true;
            playerInfo = "White";


            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            table.loadContent(Content);
            font = Content.Load<SpriteFont>("Images\\Calibri48");
            winBackground = Content.Load<Texture2D>("Images\\win");
            // TODO: use this.Content to load your game content here
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();


            mouseStateCurrent = Mouse.GetState();

            if (win)
            {
                if (mouseStateCurrent.LeftButton == ButtonState.Pressed && mouseStatePrevious.LeftButton == ButtonState.Released)
                {
                    table.initField();
                    win = false;
                    mouseStatePrevious = Mouse.GetState();
                }

            }

            switch (moveState)
            {
                case MoveState.PLAYER:
                    playerInfo = "White";
                    if (mouseStateCurrent.LeftButton == ButtonState.Pressed && mouseStatePrevious.LeftButton == ButtonState.Released)
                    {
                        Checker newChecker = table.putChecker(mouseStateCurrent, FieldType.WHITE);
                        if (newChecker != null)
                        {
                            if (table.checkState(newChecker.indexX, newChecker.indexY, newChecker.type))
                            {
                                win = true;
                                winInfo = String.Format("{0} wins!", playerInfo);
                            }
                            else
                            {
                                moveState = MoveState.AI;
                            }
                        }
                        lastPuttedChecker = newChecker;
                    }
                    break;

                case MoveState.AI:
                    playerInfo = "Black";
                    Vector2 lastplaced2 = aiPlayer.generateMove();
                    lastPuttedChecker = table.putAIChecker(lastplaced2, FieldType.BLACK);
                    aiPlayer.clearPopulation();

                    if (table.checkState(lastPuttedChecker.indexX, lastPuttedChecker.indexY, lastPuttedChecker.type))
                    {
                        win = true;
                        winInfo = String.Format("{0} wins!", playerInfo);
                        moveState = MoveState.PLAYER;
                    }
                    else
                    {
                        moveState = MoveState.PLAYER;
                    }
                    break;

            }

            mouseStatePrevious = mouseStateCurrent;
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.SaddleBrown);

            // TODO: Add your drawing code here
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);



            Vector2 FontOrigin = font.MeasureString("- Gomoku - ") / 2;
            // Draw the string
            spriteBatch.DrawString(font, "- Gomoku -", new Vector2(290, 30), new Color(87, 6, 14),
                0, FontOrigin, 1.0f, SpriteEffects.None, 0.5f);


            if (win)
            {
                Vector2 FontOrigin2 = font.MeasureString(winInfo) / 2;
                // Draw the string
                spriteBatch.DrawString(font, winInfo, new Vector2(graphics.PreferredBackBufferWidth / 2, 540), Color.White,
                    0, FontOrigin2, 1.0f, SpriteEffects.None, 0.5f);
            }
            table.draw(spriteBatch);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
